<template>
  <div v-loading.lock="loading" class="base-content">
    <div class="basic-info">
      <el-form :inline="true" :model="queryForm">
        <el-form-item :label="dataList.RoleId">
          <el-input v-model="queryForm.roleId" :placeholder="dataList.RoleId" @keyup.enter.native="onQuery" />
        </el-form-item>
        <el-form-item :label="dataList.ButtonId">
          <el-input v-model="queryForm.buttonId" :placeholder="dataList.ButtonId" @keyup.enter.native="onQuery" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onQuery">{{ btn.Query }}</el-button>
          <el-button type="primary" @click="onNew">{{ btn.Add }}</el-button>
        </el-form-item>
      </el-form>
      <el-table :data="list" element-loading-text="Loading" fit highlight-current-row>
        <el-table-column align="center" :label="dataList.number" width="50">
          <template slot-scope="scope">{{ scope.$index + 1 }}</template>
        </el-table-column>
        <el-table-column :label="dataList.RoleId">
          <template slot-scope="scope">{{ scope.row.roleId }}</template>
        </el-table-column>
        <el-table-column :label="dataList.ButtonId">
          <template slot-scope="scope">{{ scope.row.buttonId }}</template>
        </el-table-column>
        <el-table-column :label="btn.operate">
          <template slot-scope="scope">
            <div class="base-table-operation">
              <el-link size="mini" type="primary" @click="handleEdit(scope.row)">{{ btn.Edit }}</el-link>
              <el-link size="mini" type="danger" @click="handleDelete(scope.row)">{{ btn.Delete }}</el-link>
            </div>
          </template>
        </el-table-column>
      </el-table>

      <BasePagination
        :total="pagination.total"
        :page-index="pagination.pageIndex"
        :page-size="pagination.pageSize"
        @sizeChange="sizeChange"
        @changePage="changePage"
      />
    </div>

    <PermissionRoleButtonEdit
      ref="PermissionRoleButtonEdit"
      :dialog-form-visible.sync="dialogFormVisible"
    />
  </div>
</template>

<script>
import { deleteRecord, getList } from "../../../api/Permission/PermissionRoleButton.js";
import PermissionRoleButtonEdit from "./PermissionRoleButtonEdit.vue";

export default {
  name: "PermissionRoleButtonList",
  components: { PermissionRoleButtonEdit },
  data() {
    return {
      loading: true,
      list: [],
      queryForm: {
        roleId: 0,
        buttonId: 0,
      },
      dialogFormVisible: false,
      pagination: {
        pageIndex: 1,
        pageSize: 10,
        total: 0,
      },
    };
  },
  computed: {
    btn() {
      return this.$t("Permission.buttons");
    },
    dataList() {
      return this.$t("Permission.PermissionUser");
    },
  },
  created() {
    this.getList();
  },
  methods: {
    changePage(val) {
      this.pagination.pageIndex = val;
      this.getList();
    },
    sizeChange(val) {
      this.pagination.pageIndex = 1;
      this.pagination.pageSize = val;
      this.getList();
    },
    getList() {
      this.loading = true;
      getList(this.queryForm, this.pagination)
        .then((res) => {
          this.list = res.data.dataList ? res.data.dataList : [];
          this.pagination.total = res.data.total;
          this.loading = false;
        })
        .catch(() => {
          this.loading = false;
        });
    },
    onNew() {
      this.$refs.PermissionRoleButtonEdit.formData = this.Helper.deepCopy(
        this.$refs.PermissionRoleButtonEdit.initData
      );
      this.dialogFormVisible = true;
    },
    handleDelete(rowData) {
      this.Helper.deleteConfirm(() => {
        this.loading = true;
        deleteRecord({ id: rowData.id })
          .then((res) => {
            this.getList();
            this.loading = false;
          })
          .catch(() => {
            this.loading = false;
          });
      });
    },
    handleEdit(rowData) {
      this.$refs.PermissionRoleButtonEdit.formData = rowData;
      this.dialogFormVisible = true;
    },
    onQuery() {
      this.getList();
    },
  },
};
</script>
